//
//  HomeViewController.swift
//  EasyDana
//
//  Created by mac on 2024/10/9.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class HomeViewController: UIViewController {

    private let bag = DisposeBag()
    
    private let data = BehaviorRelay<HomeDataModel?>.init(value: nil)
    
    private var source: [HomeDataModel.OrderTillyModel.StuffModel] = []
    
    private let refreshing = BehaviorRelay(value: false)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configView()
        // Do any additional setup after loading the view.
    }
    
    private func configView() {
        NotificationCenter.default.rx.notification(Notification.Name("kLoginSuccess"))
            .subscribe(onNext: { [weak self] not in
                self?.getData()
            })
            .disposed(by: self.bag)
        let tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.view.addSubview(tableView)
        tableView.refreshControl = UIRefreshControl(frame: CGRect(origin: .zero, size: CGSize(width: 32, height: 32)))
        self.refreshing.bind(to: tableView.refreshControl!.rx.isRefreshing).disposed(by: self.bag)
        tableView.refreshControl?.addTarget(self, action: #selector(self.getData), for: .valueChanged)
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "HomeLoanCell", bundle: Bundle.main), forCellReuseIdentifier: "HomeLoanCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        tableView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(self.view.snp.bottomMargin)
        }
        tableView.contentInsetAdjustmentBehavior = .never
        let bigCard = { [weak self] in
            guard let sself = self else { return }
            let headerView = UIView()
            //小卡高度422
            sself.view.backgroundColor = UIColor.color(with: 247, green: 247, blue: 247)
            tableView.backgroundColor = sself.view.backgroundColor
            headerView.backgroundColor = sself.view.backgroundColor
            let topBGView = UIImageView(image: UIImage(named: "home_top_bg"))
            headerView.addSubview(topBGView)
            topBGView.snp.makeConstraints { make in
                make.top.left.right.equalToSuperview()
            }
            let topTitleView = UIImageView(image: UIImage(named: "home_top_title"))
            headerView.addSubview(topTitleView)
            topTitleView.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(88)
                make.left.equalToSuperview().offset(30)
            }
            let topAlertView = UIImageView(image: UIImage(named: "home_top_alert"))
            headerView.addSubview(topAlertView)
            topAlertView.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(64)
                make.right.equalToSuperview().offset(-15)
            }
            let loanCardView = UIView()
            headerView.addSubview(loanCardView)
            loanCardView.layer.masksToBounds = true
            loanCardView.layer.cornerRadius = 16
            loanCardView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(190)
                make.width.equalTo(345)
                make.height.equalTo(232)
            }
            let loanCardImageView = UIImageView(image: UIImage(named: "home_loan_amount_bg"))
            loanCardView.addSubview(loanCardImageView)
            loanCardImageView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            let loanTitleView = UILabel()
            loanCardView.addSubview(loanTitleView)
            loanTitleView.textColor = UIColor.color(with: 0, green: 74, blue: 222)
            loanTitleView.font = UIFont(name: "Righteous-Regular", size: 20)
            loanTitleView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(20)
            }
            
            headerView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: sself.view.frame.width, height: 1250))
            let serviceEntryView = Bundle.main.loadNibNamed("HomeEntryView", owner: nil)!.first as! HomeEntryView
            headerView.addSubview(serviceEntryView)
            serviceEntryView.snp.makeConstraints { make in
                make.top.equalTo(loanCardView.snp.bottom).offset(15)
                make.width.equalTo(345)
                make.centerX.equalToSuperview()
                make.height.equalTo(90)
            }
            serviceEntryView.layer.masksToBounds = true
            serviceEntryView.layer.cornerRadius = 14
            serviceEntryView.backgroundColor = UIColor.white
            serviceEntryView.layer.shadowColor = UIColor.black.cgColor
            serviceEntryView.layer.shadowOpacity = 0.1
            serviceEntryView.layer.shadowRadius = 4
            serviceEntryView.onService = {
                let url = sself.data.value?.hastings.theatre
                let webVC = WebViewController()
                webVC.url = url
                webVC.hidesBottomBarWhenPushed = true
                sself.navigationController?.pushViewController(webVC, animated: true)
            }
            serviceEntryView.onLoanMaterial = {
                let materialVC = LoanMaterialViewController()
                materialVC.hidesBottomBarWhenPushed = true
                sself.navigationController?.pushViewController(materialVC, animated: true)
            }
            serviceEntryView.onQA = {
                let materialVC = QAViewController()
                materialVC.hidesBottomBarWhenPushed = true
                sself.navigationController?.pushViewController(materialVC, animated: true)
            }
            serviceEntryView.onFeedBack = {
                let url = sself.data.value?.hastings.ceria
                let webVC = WebViewController()
                webVC.url = url
                webVC.hidesBottomBarWhenPushed = true
                sself.navigationController?.pushViewController(webVC, animated: true)
            }
            let marqueeView = Bundle.main.loadNibNamed("MarqueeView", owner: nil)?.first as! MarqueeView
            headerView.addSubview(marqueeView)
            marqueeView.layer.masksToBounds = true
            marqueeView.layer.cornerRadius = 18
            
            sself.data
                .filter({$0 != nil})
                .map { model in
                    return model!.minuet
                }
                .bind(to: marqueeView.source)
                .disposed(by: sself.bag)
            marqueeView.snp.makeConstraints { make in
                make.top.equalTo(serviceEntryView.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
                make.width.equalTo(345)
                make.height.equalTo(36)
            }
            let homeADView = UIImageView(image: UIImage(named: "home_ad"))
            headerView.addSubview(homeADView)
            homeADView.snp.makeConstraints { make in
                make.top.equalTo(marqueeView.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
            }
            let processView = UIImageView(image: UIImage(named: "home_process"))
            headerView.addSubview(processView)
            processView.snp.makeConstraints { make in
                make.top.equalTo(homeADView.snp.bottom).offset(15)
                make.centerX.equalToSuperview()
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.proves
                    default:
                        return ""
                    }
                }
                .bind(to: loanTitleView.rx.text)
                .disposed(by: sself.bag)
            let loanAmountLabel = UILabel()
            loanCardView.addSubview(loanAmountLabel)
            loanAmountLabel.textColor = UIColor.color(with: 0, green: 74, blue: 222)
            loanAmountLabel.font = UIFont(name: "Righteous-Regular", size: 40)
            loanAmountLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(loanTitleView.snp.bottom).offset(12)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.painted
                    default:
                        return ""
                    }
                }
                .bind(to: loanAmountLabel.rx.text)
                .disposed(by: sself.bag)
            let dateLabel = UILabel()
            loanCardView.addSubview(dateLabel)
            dateLabel.textColor = loanAmountLabel.textColor
            dateLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            dateLabel.snp.makeConstraints { make in
                make.top.equalTo(loanAmountLabel.snp.bottom).offset(10)
                make.left.equalToSuperview().offset(98)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.terrible
                    default:
                        return ""
                    }
                }
                .bind(to: dateLabel.rx.text)
                .disposed(by: sself.bag)
            let dateDescLabel = UILabel()
            loanCardView.addSubview(dateDescLabel)
            dateDescLabel.textColor = UIColor.color(with: 41, green: 112, blue: 255)
            dateDescLabel.font = UIFont.systemFont(ofSize: 14)
            dateDescLabel.snp.makeConstraints { make in
                make.top.equalTo(dateLabel.snp.bottom).offset(6)
                make.centerX.equalTo(dateLabel)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.wed
                    default:
                        return ""
                    }
                }
                .bind(to: dateDescLabel.rx.text)
                .disposed(by: sself.bag)
            let rateLabel = UILabel()
            loanCardView.addSubview(rateLabel)
            rateLabel.textColor = loanAmountLabel.textColor
            rateLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            rateLabel.snp.makeConstraints { make in
                make.top.equalTo(loanAmountLabel.snp.bottom).offset(10)
                make.right.equalToSuperview().offset(-50)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.cost
                    default:
                        return ""
                    }
                }
                .bind(to: rateLabel.rx.text)
                .disposed(by: sself.bag)
            
            let rateDescLabel = UILabel()
            loanCardView.addSubview(rateDescLabel)
            rateDescLabel.textColor = UIColor.color(with: 41, green: 112, blue: 255)
            rateDescLabel.font = UIFont.systemFont(ofSize: 14)
            rateDescLabel.snp.makeConstraints { make in
                make.top.equalTo(rateLabel.snp.bottom).offset(6)
                make.centerX.equalTo(rateLabel)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.sent
                    default:
                        return ""
                    }
                }
                .bind(to: rateDescLabel.rx.text)
                .disposed(by: sself.bag)
            
            let applyButton = UIButton(type: .custom)
            loanCardView.addSubview(applyButton)
            applyButton.setBackgroundImage(UIImage(named: "home_apply_button"), for: .normal)
            applyButton.snp.makeConstraints { make in
                make.top.equalTo(rateDescLabel.snp.bottom).offset(10)
                make.centerX.equalToSuperview()
            }
            applyButton.rx.tap
                .subscribe(onNext: { [weak self] event in
                    let tilly = self?.data.value?.tilly.filter({ model in
                        switch model {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }).first
                    switch tilly {
                    case .loan(let loan):
                        let id = loan.stuffs.first?.genteel ?? -1
                        self?.applyAction(id: "\(id)")
                        break
                    default:
                        break
                    }
                    
                })
                .disposed(by: sself.bag)
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    var title = ""
                    switch loan {
                    case .loan(let loan):
                        title = loan.stuffs.first?.evenings ?? ""
                        break
                    default:
                        break
                    }
                    return NSAttributedString(string: title, attributes: [.foregroundColor: UIColor.white, .font: UIFont(name: "Righteous-Regular", size: 20)!])
                }
                .bind(to: applyButton.rx.attributedTitle(for: .normal))
                .disposed(by: sself.bag)
            tableView.tableHeaderView = headerView
        }
        let smallCard = { [weak self] in
            guard let sself = self else { return }
            let headerView = UIView()
            tableView.tableHeaderView = headerView
            //小卡高度422
            sself.view.backgroundColor = UIColor.color(with: 247, green: 247, blue: 247)
            tableView.backgroundColor = self?.view.backgroundColor
            headerView.backgroundColor = self?.view.backgroundColor
            
            let topBGView = UIImageView(image: UIImage(named: "home_top_bg"))
            headerView.addSubview(topBGView)
            topBGView.snp.makeConstraints { make in
                make.top.left.right.equalToSuperview()
            }
            let topTitleView = UIImageView(image: UIImage(named: "home_top_title"))
            headerView.addSubview(topTitleView)
            topTitleView.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(88)
                make.left.equalToSuperview().offset(30)
            }
            let topAlertView = UIImageView(image: UIImage(named: "home_top_alert"))
            headerView.addSubview(topAlertView)
            topAlertView.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(64)
                make.right.equalToSuperview().offset(-15)
            }
            let loanCardView = UIView()
            headerView.addSubview(loanCardView)
            loanCardView.layer.masksToBounds = true
            loanCardView.layer.cornerRadius = 16
            loanCardView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(190)
                make.width.equalTo(345)
                make.height.equalTo(232)
            }
            let loanCardImageView = UIImageView(image: UIImage(named: "home_loan_amount_bg"))
            loanCardView.addSubview(loanCardImageView)
            loanCardImageView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            let loanTitleView = UILabel()
            loanCardView.addSubview(loanTitleView)
            loanTitleView.textColor = UIColor.color(with: 0, green: 74, blue: 222)
            loanTitleView.font = UIFont(name: "Righteous-Regular", size: 20)
            loanTitleView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(20)
            }
            headerView.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: sself.view.frame.width, height: 400))
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.proves
                    default:
                        return ""
                    }
                }
                .bind(to: loanTitleView.rx.text)
                .disposed(by: sself.bag)
            let loanAmountLabel = UILabel()
            loanCardView.addSubview(loanAmountLabel)
            loanAmountLabel.textColor = UIColor.color(with: 0, green: 74, blue: 222)
            loanAmountLabel.font = UIFont(name: "Righteous-Regular", size: 40)
            loanAmountLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(loanTitleView.snp.bottom).offset(12)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.painted
                    default:
                        return ""
                    }
                }
                .bind(to: loanAmountLabel.rx.text)
                .disposed(by: sself.bag)
            let dateLabel = UILabel()
            loanCardView.addSubview(dateLabel)
            dateLabel.textColor = loanAmountLabel.textColor
            dateLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            dateLabel.snp.makeConstraints { make in
                make.top.equalTo(loanAmountLabel.snp.bottom).offset(10)
                make.left.equalToSuperview().offset(98)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.terrible
                    default:
                        return ""
                    }
                }
                .bind(to: dateLabel.rx.text)
                .disposed(by: sself.bag)
            let dateDescLabel = UILabel()
            loanCardView.addSubview(dateDescLabel)
            dateDescLabel.textColor = UIColor.color(with: 41, green: 112, blue: 255)
            dateDescLabel.font = UIFont.systemFont(ofSize: 14)
            dateDescLabel.snp.makeConstraints { make in
                make.top.equalTo(dateLabel.snp.bottom).offset(6)
                make.centerX.equalTo(dateLabel)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.wed
                    default:
                        return ""
                    }
                }
                .bind(to: dateDescLabel.rx.text)
                .disposed(by: sself.bag)
            let rateLabel = UILabel()
            loanCardView.addSubview(rateLabel)
            rateLabel.textColor = loanAmountLabel.textColor
            rateLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            rateLabel.snp.makeConstraints { make in
                make.top.equalTo(loanAmountLabel.snp.bottom).offset(10)
                make.right.equalToSuperview().offset(-50)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.cost
                    default:
                        return ""
                    }
                }
                .bind(to: rateLabel.rx.text)
                .disposed(by: sself.bag)
            
            let rateDescLabel = UILabel()
            loanCardView.addSubview(rateDescLabel)
            rateDescLabel.textColor = UIColor.color(with: 41, green: 112, blue: 255)
            rateDescLabel.font = UIFont.systemFont(ofSize: 14)
            rateDescLabel.snp.makeConstraints { make in
                make.top.equalTo(rateLabel.snp.bottom).offset(6)
                make.centerX.equalTo(rateLabel)
            }
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    switch loan {
                    case .loan(let loan):
                        return loan.stuffs.first?.sent
                    default:
                        return ""
                    }
                }
                .bind(to: rateDescLabel.rx.text)
                .disposed(by: sself.bag)
            
            let applyButton = UIButton(type: .custom)
            loanCardView.addSubview(applyButton)
            applyButton.setBackgroundImage(UIImage(named: "home_apply_button"), for: .normal)
            applyButton.snp.makeConstraints { make in
                make.top.equalTo(rateDescLabel.snp.bottom).offset(10)
                make.centerX.equalToSuperview()
            }
            applyButton.rx.tap
                .subscribe(onNext: { [weak self] event in
                    let tilly = self?.data.value?.tilly.filter({ model in
                        switch model {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }).first
                    switch tilly {
                    case .loan(let loan):
                        let id = loan.stuffs.first?.genteel ?? -1
                        self?.applyAction(id: "\(id)")
                        break
                    default:
                        break
                    }
                    
                })
                .disposed(by: sself.bag)
            sself.data
                .filter({$0 != nil})
                .map { model in
                    let loan = model!.tilly.filter { m in
                        switch m {
                        case .loan(_):
                            return true
                        default:
                            return false
                        }
                    }.first
                    var title = ""
                    switch loan {
                    case .loan(let loan):
                        title = loan.stuffs.first?.evenings ?? ""
                        break
                    default:
                        break
                    }
                    return NSAttributedString(string: title, attributes: [.foregroundColor: UIColor.white, .font: UIFont(name: "Righteous-Regular", size: 20)!])
                }
                .bind(to: applyButton.rx.attributedTitle(for: .normal))
                .disposed(by: sself.bag)
        }
        self.data
            .filter({$0 != nil})
            .subscribe(onNext: { [weak self] model in
                let loan = model!.tilly.filter { m in
                    switch m {
                    case .loan(_):
                        return true
                    default:
                        return false
                    }
                }.first
                if loan != nil {
                    switch loan! {
                    case .loan(let m):
                        if m.cordially == "fond" {
                            bigCard()
                        } else {
                            smallCard()
                            let order = model!.tilly.filter { m in
                                switch m {
                                case .order(_):
                                    return true
                                default:
                                    return false
                                }
                            }.first
                            if order != nil {
                                switch order! {
                                case .order(let orders):
                                    self?.source = orders.stuffs
                                    tableView.reloadData()
                                    break
                                default:
                                    break
                                }
                            }
                        }
                        break
                    default:
                        break
                    }
                }
            })
            .disposed(by: self.bag)
        
    }
    
    @objc private func getData() {
        Task {
            do {
                self.showLoading(text: nil)
                UploadStasticManager.default.uploadDataWhenLaunchAndHome(home: true)
                let res = try await HTTPManager.default.get(path: Api.home, param: nil, dataType: HomeDataModel.self)
                self.hideLoading()
                self.data.accept(res.data)
                self.refreshing.accept(false)
            } catch {
                self.hideLoading()
                self.refreshing.accept(false)
                print(error)
            }
        }
    }
    
    //申请贷款
    private func applyAction(id: String) {
        Task {
            if HTTPManager.default.sessionID.count == 0 {
                GlobalNotifier.default.needLogin.onNext(true)
                return
            }
            do {
                struct ApplyModel: Codable {
                    let connubial: Int
                    let drank: String
                    let cordially: Int?
                    let shrine: String
                }
                self.showLoading(text: nil)
                let res = try await HTTPManager.default.post(path: Api.preorder, param: ["guns": id], dataType: ApplyModel.self)
                self.hideLoading()
                if (res.code == 0) {
                    let u = res.data!.drank
                    if let url = URL(string: u) {
                        self.jump(url: url)
                    }
                } else {
                    self.show(toast: res.state)
                }
            } catch {
                print(error)
                self.hideLoading()
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.getData()
    }

}

extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeLoanCell") as! HomeLoanCell
        cell.model = self.source[indexPath.row]
        cell.detailAction = { model in
            let gentel = model?.genteel ?? 0
            self.applyAction(id: "\(gentel)")
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.source.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 164
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if self.source.count == 0 {
            return 0
        } else {
            return 46
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if self.source.count == 0 {
            return nil
        } else {
            let view = UIView()
            view.backgroundColor = tableView.backgroundColor
            let imageView = UIImageView(image: UIImage(named: "home_loan_supermarket"))
            view.addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(15)
                make.centerY.equalToSuperview()
            }
            return view
        }
    }
}
